﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;
using web.component;

namespace web.bussines
{
    public class web_link_service : base_service
    {
        //get all get all
        public PagedList<web_link>  get_plist(int pageindex, int pagesize, int language_id, string link_type)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_web_link
                            where dt.language_id == language_id && dt.type == link_type 
                            select dt);
                list = list.OrderBy(p => p.order_display);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        public List<web_link>  list_by_type(int language_id, string link_type)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_web_link
                            where dt.type == link_type && dt.language_id == language_id
                            select dt);
                list = list.OrderBy(p => p.order_display);
                return list.ToList<web_link>();
            }
        }

        public int weblink_update(web_link it)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_web_link.Where(p => p.link_id == it.link_id).FirstOrDefault();
                if (item != null)
                {
                    item.link_id = it.link_id;
                    item.title = it.title;
                    item.link = it.link;
                    item.type = it.type;
                    item.order_display = it.order_display;
                    item.description = it.description;
                    item.image_path = it.image_path;
                    item.language_id = it.language_id; 

                    data.SaveChanges();
                    return item.link_id;
                }
                else
                {
                    data.AddTotbl_web_link(it);
                    data.SaveChanges();
                    return it.link_id;
                }
            }
        }
        
        //get items by id or code
        public web_link getByID(int id)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_web_link.Where(p => p.link_id == id).FirstOrDefault();
              //  if (item == null)
               //     item = new web_link();
                return item;
            }
        }

        //delete by id
        public void deleteItem(int id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_web_link.Where(p => p.link_id == id).FirstOrDefault();

                if (em != null)
                {
                    data.DeleteObject(em);
                    data.SaveChanges();
                }
            }
        }

    }
}
